//
//  InAppPurchaseManager.m
//  iStock Global
//
//  Created by Andy on 2011/1/25.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Foundation/NSString.h>
#import "InAppPurchaseManager.h"
#import "JSON.h"
 
//#import "SBJsonBase.h"
 

//標記:Add Target need Program 
#define upperlimit_100 @"com.xxtradexx.iStockGlobal.100" //後面的數字可以用來分析記錄最大數目
#define upperlimit_1000 @"com.xxtradexx.iStockGlobal.1000"
#define upperlimit_10000 @"com.xxtradexx.iStockGlobal.10000"


#define ETF_upperlimit_100 @"com.xxtradexx.etf.100" //後面的數字可以用來分析記錄最大數目
#define ETF_upperlimit_1000 @"com.xxtradexx.etf.1000"
#define ETF_upperlimit_10000 @"com.xxtradexx.etf.10000"

#define HK_upperlimit_50 @"com.xxtradexx.hk.50" //後面的數字可以用來分析記錄最大數目
#define HK_upperlimit_100 @"com.xxtradexx.hk.100"
#define HK_upperlimit_1000 @"com.xxtradexx.hk.1000"

#define CH_upperlimit_50 @"com.xxtradexx.ch.50" //後面的數字可以用來分析記錄最大數目
#define CH_upperlimit_100 @"com.xxtradexx.ch.100"
#define CH_upperlimit_1000 @"com.xxtradexx.ch.1000"

#define US_upperlimit_50 @"com.xxtradexx.us.50" //後面的數字可以用來分析記錄最大數目
#define US_upperlimit_100 @"com.xxtradexx.us.100"
#define US_upperlimit_1000 @"com.xxtradexx.us.1000"

#define UK_upperlimit_50 @"com.xxtradexx.uk.50" //後面的數字可以用來分析記錄最大數目
#define UK_upperlimit_100 @"com.xxtradexx.uk.101"
#define UK_upperlimit_1000 @"com.xxtradexx.uk.1000"

#define CN_upperlimit_50 @"com.xxtradexx.cn.50" //後面的數字可以用來分析記錄最大數目
#define CN_upperlimit_100 @"com.xxtradexx.cn.100"
#define CN_upperlimit_1000 @"com.xxtradexx.cn.1000"

#define KS_upperlimit_50 @"com.xxtradexx.ks.50" //後面的數字可以用來分析記錄最大數目
#define KS_upperlimit_100 @"com.xxtradexx.ks.100"
#define KS_upperlimit_1000 @"com.xxtradexx.ks.1000"

#define iPadiStockGlobal_upperlimit_50 @"com.xxtradexx.iPadiStockGlobal.50" 
#define iPadiStockGlobal_upperlimit_100 @"com.xxtradexx.iPadiStockGlobal.100" 
#define iPadiStockGlobal_upperlimit_1000 @"com.xxtradexx.iPadiStockGlobal.1000" 

#define iPadHK_upperlimit_50 @"com.xxtradexx.iPadHK.50" 
#define iPadHK_upperlimit_100 @"com.xxtradexx.iPadHK.100" 
#define iPadHK_upperlimit_1000 @"com.xxtradexx.iPadHK.1000" 

#define SI_upperlimit_50 @"com.xxtradexx.iPhoneSI.50" //後面的數字可以用來分析記錄最大數目
#define SI_upperlimit_100 @"com.xxtradexx.iPhoneSI.100"
#define SI_upperlimit_1000 @"com.xxtradexx.iPhoneSI.1000"

#define iPadCH_upperlimit_50 @"com.xxtradexx.iPadCH.50" 
#define iPadCH_upperlimit_100 @"com.xxtradexx.iPadCH.100" 
#define iPadCH_upperlimit_1000 @"com.xxtradexx.iPadCH.1000" 

#define AX_upperlimit_50 @"com.xxtradexx.iPhoneAX.50" //後面的數字可以用來分析記錄最大數目
#define AX_upperlimit_100 @"com.xxtradexx.iPhoneAX.100"
#define AX_upperlimit_1000 @"com.xxtradexx.iPhoneAX.1000"

#define GR_upperlimit_50 @"com.xxtradexx.gr.50" //後面的數字可以用來分析記錄最大數目
#define GR_upperlimit_100 @"com.xxtradexx.gr.100"
#define GR_upperlimit_1000 @"com.xxtradexx.gr.1000"

#define PA_upperlimit_50 @"com.xxtradexx.pa.50" //後面的數字可以用來分析記錄最大數目
#define PA_upperlimit_100 @"com.xxtradexx.pa.100"
#define PA_upperlimit_1000 @"com.xxtradexx.pa.1000"

#define iPadCN_upperlimit_50 @"canada.iPad.lf.50" 
#define iPadCN_upperlimit_100 @"canada.iPad.lf.100" 
#define iPadCN_upperlimit_1000 @"canada.iPad.lf.1000" 

#define iPadGR_upperlimit_50 @"gr.iPad.lf.50" 
#define iPadGR_upperlimit_100 @"gr.iPad.lf.100" 
#define iPadGR_upperlimit_1000 @"gr.iPad.lf.1000" 


#define iPadPA_upperlimit_50 @"pa.iPad.lf.50" 
#define iPadPA_upperlimit_100 @"pa.iPad.lf.100" 
#define iPadPA_upperlimit_1000 @"pa.iPad.lf.1000" 


#define iPadAX_upperlimit_50 @"com.xxtradexx.iPadAX.50" 
#define iPadAX_upperlimit_100 @"com.xxtradexx.iPadAX.100" 
#define iPadAX_upperlimit_1000 @"com.xxtradexx.iPadAX.1000" 

#define iPadUK_upperlimit_50 @"com.xxtradexx.iPadUK.50" 
#define iPadUK_upperlimit_100 @"Com.xxtradexx.iPadUK.100" 
#define iPadUK_upperlimit_1000 @"Com.xxtradexx.iPadUK.1000"

#define iPadETF_upperlimit_50 @"com.xxtradexx.iPadETF.50" 
#define iPadETF_upperlimit_100 @"com.xxtradexx.iPadETF.100" 
#define iPadETF_upperlimit_1000 @"com.xxtradexx.iPadETF.1000"

#define iPadSI_upperlimit_50 @"com.xxtradexx.iPadSI.50" 
#define iPadSI_upperlimit_100 @"com.xxtradexx.iPadSI.100" 
#define iPadSI_upperlimit_1000 @"com.xxtradexx.iPadSI.1000"

#define iPadUS_upperlimit_50 @"com.xxtradexx.iPadUS.50" 
#define iPadUS_upperlimit_100 @"com.xxtradexx.iPadUS.100" 
#define iPadUS_upperlimit_1000 @"com.xxtradexx.iPadUS.1000"


#define iPadINDIA_upperlimit_50 @"com.xxtradexx.iPadINDIA.50" 
#define iPadINDIA_upperlimit_100 @"com.xxtradexx.iPadINDIA.100" 
#define iPadINDIA_upperlimit_1000 @"com.xxtradexx.iPadINDIA.1000"


#define iPhoneINDIA_upperlimit_50 @"com.xxtradexx.iPhoneINDIA.50" 
#define iPhoneINDIA_upperlimit_100 @"com.xxtradexx.iPhoneINDIA.100" 
#define iPhoneINDIA_upperlimit_1000 @"com.xxtradexx.iPhoneINDIA.1000"

#define iPadNZ_upperlimit_50 @"com.xxtradexx.iPadNZ.50" 
#define iPadNZ_upperlimit_100 @"com.xxtradexx.iPadNZ.100" 
#define iPadNZ_upperlimit_1000 @"com.xxtradexx.iPadNZ.1000"


#define iPhoneNZ_upperlimit_50 @"com.xxtradexx.iPoneNZ.50" 
#define iPhoneNZ_upperlimit_100 @"com.xxtradexx.iPoneNZ.100" 
#define iPhoneNZ_upperlimit_1000 @"com.xxtradexx.iPoneNZ.1000"

#define iPadMI_upperlimit_50 @"com.xxtradexx.iPadMI.50" 
#define iPadMI_upperlimit_100 @"com.xxtradexx.iPadMI.100" 
#define iPadMI_upperlimit_1000 @"com.xxtradexx.iPadMI.1000"

#define iPhoneMI_upperlimit_50 @"com.xxtradexx.iPhoneMI.50" 
#define iPhoneMI_upperlimit_100 @"com.xxtradexx.iPhoneMI.100" 
#define iPhoneMI_upperlimit_1000 @"com.xxtradexx.iPhoneMI.1000"

#define iPadSA_upperlimit_50 @"com.xxtradexx.iPadSA.50" 
#define iPadSA_upperlimit_100 @"com.xxtradexx.iPadSA.100" 
#define iPadSA_upperlimit_1000 @"com.xxtradexx.iPadSA.1000"

#define iPhoneSA_upperlimit_50 @"com.xxtradexx.iPoneSA.50" 
#define iPhoneSA_upperlimit_100 @"com.xxtradexx.iPoneSA.100" 
#define iPhoneSA_upperlimit_1000 @"com.xxtradexx.iPoneSA.1000"

#define iPadMX_upperlimit_50 @"com.xxtradexx.iPadMX.50" 
#define iPadMX_upperlimit_100 @"com.xxtradexx.iPadMX.100" 
#define iPadMX_upperlimit_1000 @"com.xxtradexx.iPadMX.1000"

#define iPhoneMX_upperlimit_50 @"com.xxtradexx.iPoneMX.50" 
#define iPhoneMX_upperlimit_100 @"com.xxtradexx.iPoneMX.100" 
#define iPhoneMX_upperlimit_1000 @"com.xxtradexx.iPoneMX.1000"

#define iPhoneGlobalMX_upperlimit_3 @"2D.Basic.MESA.iPhone.Global.3month" 
#define iPhoneGlobalMX_upperlimit_7 @"2D.Basic.MESA.iPhone.Global.7days" 

#define UniversalAX_upperlimit_50 @"com.xxtradexx.UniversalAX.50" 
#define UniversalAX_upperlimit_100 @"com.xxtradexx.UniversalAX.100" 
#define UniversalAX_upperlimit_1000 @"com.xxtradexx.UniversalAX.1000"
#define UniversalAX_upperlimit_3month @"com.xxtradexx.UniversalAX.3month"

#define UniversalUK_upperlimit_50 @"com.xxtradexx.UniversalUK.50" 
#define UniversalUK_upperlimit_100 @"com.xxtradexx.UniversalUKa.100" 
#define UniversalUK_upperlimit_1000 @"com.xxtradexx.UniversalUK.1000"

#define UniversalUS_upperlimit_50 @"com.xxtradexx.UniversalUS.50" 
#define UniversalUS_upperlimit_100 @"com.xxtradexx.UniversalUS.100" 
#define UniversalUS_upperlimit_1000 @"com.xxtradexx.UniversalUS.1000"

#define UniversalHK_upperlimit_50 @"com.xxtradexx.UniversalHK.50" 
#define UniversalHK_upperlimit_100 @"com.xxtradexx.UniversalHK.100" 
#define UniversalHK_upperlimit_1000 @"com.xxtradexx.UniversalHK.1000"

#define UniversalCH_upperlimit_50 @"com.xxtradexx.UniversalCH.50" 
#define UniversalCH_upperlimit_100 @"com.xxtradexx.UniversalCH.100" 
#define UniversalCH_upperlimit_1000 @"com.xxtradexx.UniversalCH.1000"

#define UniversalINDIA_upperlimit_50 @"com.xxtradexx.UniversalIN.50" 
#define UniversalINDIA_upperlimit_100 @"com.xxtradexx.UniversalIN.100" 
#define UniversalINDIA_upperlimit_1000 @"com.xxtradexx.UniversalIN.1000"


//#define StoreURL  @"https://sandbox.itunes.apple.com/verifyReceipt"

/*
#ifndef NDEBUG
#define StoreURL @"https://sandbox.itunes.apple.com/verifyReceipt"
#else
#define StoreURL @"https://buy.itunes.apple.com/verifyReceipt"
#endif
*/

//標記:Add Target need Program  收據檢查的網址
#ifdef AppStore

#define StoreURL @"https://buy.itunes.apple.com/verifyReceipt"
 
#else
 
  
#endif   

#ifdef Development
#define StoreURL @"https://sandbox.itunes.apple.com/verifyReceipt"
#else
 
#endif    


//IAP測試帳號 andy0401@mail.pscnet.com.tw Qwerty123456
//Gmail帳號國際通用帳號 andyej0401@gmail.com Qwerty123456

@implementation InAppPurchaseManager

@synthesize delgete;
@synthesize Product_title;
@synthesize Product_description;
@synthesize Product_price;
@synthesize Product_id;



#pragma mark -
#pragma mark 初始化方法

-(id)init{
	self= [super init];
    if (self ) {
		
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
		 
		Product_title=[[NSMutableArray alloc] init];
		Product_description=[[NSMutableArray alloc] init];
		Product_price=[[NSMutableArray alloc] init];
		Product_id=[[NSMutableArray alloc] init];
       
        temp_array_transaction_Restored=[[NSMutableArray alloc] init];
        temp_array__transaction_Restored_rawdata_json=[[NSMutableArray alloc] init];
        
        array_r_bid=[[NSMutableArray alloc] init];
        array_r_original_transaction_id=[[NSMutableArray alloc] init]; 
        array_r_item_id=[[NSMutableArray alloc] init];
        array_r_transaction_id=[[NSMutableArray alloc] init];
        array_r_quantity=[[NSMutableArray alloc] init];
        array_r_bvrs=[[NSMutableArray alloc] init];
        array_r_expires_date_formatted=[[NSMutableArray alloc] init];
        array_r_original_purchase_date=[[NSMutableArray alloc] init];
         array_r_expires_date=[[NSMutableArray alloc] init];
        array_r_purchase_date=[[NSMutableArray alloc] init];
        array_r_product_id=[[NSMutableArray alloc] init];   
        
        array_lri_bid=[[NSMutableArray alloc] init];
        array_lri_original_transaction_id=[[NSMutableArray alloc] init]; 
        array_lri_item_id=[[NSMutableArray alloc] init];
        array_lri_transaction_id=[[NSMutableArray alloc] init];
        array_lri_quantity=[[NSMutableArray alloc] init];
        array_lri_bvrs=[[NSMutableArray alloc] init];
        array_lri_expires_date_formatted=[[NSMutableArray alloc] init];
        array_lri_original_purchase_date=[[NSMutableArray alloc] init];
        array_lri_expires_date=[[NSMutableArray alloc] init];
        array_lri_purchase_date=[[NSMutableArray alloc] init];
       array_lri_product_id=[[NSMutableArray alloc] init];        
        
        array_eri_bid=[[NSMutableArray alloc] init];
        array_eri_original_transaction_id=[[NSMutableArray alloc] init];  
        array_eri_item_id=[[NSMutableArray alloc] init];
        array_eri_transaction_id=[[NSMutableArray alloc] init];
        array_eri_quantity=[[NSMutableArray alloc] init];
        array_eri_bvrs=[[NSMutableArray alloc] init];
        array_eri_expires_date_formatted=[[NSMutableArray alloc] init];
        array_eri_original_purchase_date=[[NSMutableArray alloc] init];
        array_eri_expires_date=[[NSMutableArray alloc] init];
        array_eri_purchase_date=[[NSMutableArray alloc] init];
        array_eri_product_id=[[NSMutableArray alloc] init];      
        
        array_status=[[NSMutableArray alloc] init];     
        
	}
	return self;
    
}

#pragma mark -
#pragma mark 回復購買商品

-(void)restore_purchase{
     
     [[SKPaymentQueue defaultQueue] restoreCompletedTransactions]; 
    
}

#pragma mark -
#pragma mark 啟動網路商店上目前有哪些商品

- (void)requestProUpgradeProductData
{
    NSString *DisplayName;
    DisplayName=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]; 
    
    //標記:Add Target need Program 
    if ([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_ETF"]) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     ETF_upperlimit_100,	
                                     ETF_upperlimit_1000,								 
                                     ETF_upperlimit_10000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
    } 
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_iStock Global"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     upperlimit_100,	
                                     upperlimit_1000,								 
                                     upperlimit_10000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }                                
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_HK"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     HK_upperlimit_50,	
                                     HK_upperlimit_100,
                                     HK_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }             
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_CH"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     CH_upperlimit_50,	
                                     CH_upperlimit_100,
                                     CH_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }        
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_US"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     US_upperlimit_50,	
                                     US_upperlimit_100,
                                     US_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }            
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_UK"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     UK_upperlimit_50,	
                                     UK_upperlimit_100,
                                     UK_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_CN"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     CN_upperlimit_50,	
                                     CN_upperlimit_100,
                                     CN_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_KS"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     KS_upperlimit_50,	
                                     KS_upperlimit_100,
                                     KS_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_iStock Global"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadiStockGlobal_upperlimit_50,	
                                     iPadiStockGlobal_upperlimit_100,
                                     iPadiStockGlobal_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_HK"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadHK_upperlimit_50,	
                                     iPadHK_upperlimit_100,
                                     iPadHK_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_SI"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     SI_upperlimit_50,	
                                     SI_upperlimit_100,
                                     SI_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_CH"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadCH_upperlimit_50,	
                                     iPadCH_upperlimit_100,
                                     iPadCH_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_AX"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     AX_upperlimit_50,	
                                     AX_upperlimit_100,
                                     AX_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_GR"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     GR_upperlimit_50,	
                                     GR_upperlimit_100,
                                     GR_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_PA"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     PA_upperlimit_50,	
                                     PA_upperlimit_100,
                                     PA_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_CN"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadCN_upperlimit_50,	
                                     iPadCN_upperlimit_100,
                                     iPadCN_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_GR"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadGR_upperlimit_50,	
                                     iPadGR_upperlimit_100,
                                     iPadGR_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_PA"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadPA_upperlimit_50,	
                                     iPadPA_upperlimit_100,
                                     iPadPA_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_AX"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadAX_upperlimit_50,	
                                     iPadAX_upperlimit_100,
                                     iPadAX_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }   
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_UK"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadUK_upperlimit_50,	
                                     iPadUK_upperlimit_100,
                                     iPadUK_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }          
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_ETF"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadETF_upperlimit_50,	
                                     iPadETF_upperlimit_100,
                                     iPadETF_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }          
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_SI"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadSI_upperlimit_50,	
                                     iPadSI_upperlimit_100,
                                     iPadSI_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }      
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_US"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadUS_upperlimit_50,	
                                     iPadUS_upperlimit_100,
                                     iPadUS_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }             
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_INDIA"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadINDIA_upperlimit_50,	
                                     iPadINDIA_upperlimit_100,
                                     iPadINDIA_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }   
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_INDIA"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPhoneINDIA_upperlimit_50,	
                                     iPhoneINDIA_upperlimit_100,
                                     iPhoneINDIA_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }               
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_NZ"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadNZ_upperlimit_50,	
                                     iPadNZ_upperlimit_100,
                                     iPadNZ_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_NZ"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPhoneNZ_upperlimit_50,	
                                     iPhoneNZ_upperlimit_100,
                                     iPhoneNZ_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }           
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_MI"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadMI_upperlimit_50,	
                                     iPadMI_upperlimit_100,
                                     iPadMI_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }           
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_MI"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPhoneMI_upperlimit_50,	
                                     iPhoneMI_upperlimit_100,
                                     iPhoneMI_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }              
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_SA"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadSA_upperlimit_50,	
                                     iPadSA_upperlimit_100,
                                     iPadSA_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }       
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_SA"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPhoneSA_upperlimit_50,	
                                     iPhoneSA_upperlimit_100,
                                     iPhoneSA_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }           
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPad_MX"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPadMX_upperlimit_50,	
                                     iPadMX_upperlimit_100,
                                     iPadMX_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }  
    else if([DisplayName isEqualToString:@"2D_BASIC_LF_iPhone_MX"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPhoneMX_upperlimit_50,	
                                     iPhoneMX_upperlimit_100,
                                     iPhoneMX_upperlimit_1000,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        //iPhoneGlobalMX_upperlimit_3        
    }   
    else if([DisplayName isEqualToString:@"2D_BASIC_MESA_iPhone_Global"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     iPhoneGlobalMX_upperlimit_3 ,iPhoneGlobalMX_upperlimit_7,                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        //       
    }       
    
    else if([DisplayName isEqualToString:@"2D_Basic_MESA_Universal_AX"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     UniversalAX_upperlimit_50 ,	
                                     UniversalAX_upperlimit_100 ,
                                     UniversalAX_upperlimit_1000 ,
                                    // UniversalAX_upperlimit_3month,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }      
    else if([DisplayName isEqualToString:@"2D_Basic_MESA_Universal_UK"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     UniversalUK_upperlimit_50 ,	
                                     UniversalUK_upperlimit_100 ,
                                     UniversalUK_upperlimit_1000 ,
                                     // UniversalAX_upperlimit_3month,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }     
    else if([DisplayName isEqualToString:@"2D_Basic_MESA_Universal_US"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     UniversalUS_upperlimit_50 ,	
                                     UniversalUS_upperlimit_100 ,
                                     UniversalUS_upperlimit_1000 ,
                                     // UniversalAX_upperlimit_3month,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }     
    else if([DisplayName isEqualToString:@"2D_Basic_MESA_Universal_HK"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     UniversalHK_upperlimit_50 ,	
                                     UniversalHK_upperlimit_100 ,
                                     UniversalHK_upperlimit_1000 ,
                                     // UniversalAX_upperlimit_3month,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }         
    else if([DisplayName isEqualToString:@"2D_Basic_MESA_Universal_CH"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     UniversalCH_upperlimit_50 ,	
                                     UniversalCH_upperlimit_100 ,
                                     UniversalCH_upperlimit_1000 ,
                                     // UniversalAX_upperlimit_3month,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    } 
    else if([DisplayName isEqualToString:@"2D_Basic_MESA_Universal_CH"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     UniversalCH_upperlimit_50 ,	
                                     UniversalCH_upperlimit_100 ,
                                     UniversalCH_upperlimit_1000 ,
                                     // UniversalAX_upperlimit_3month,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }           
    else if([DisplayName isEqualToString:@"2D_Basic_MESA_Universal_INDIA"]  ) {
        //此段 是要用來啟動網路商店上目前有哪些商品
        NSSet *productIdentifiers = [NSSet setWithObjects:
                                     UniversalINDIA_upperlimit_50 ,	
                                     UniversalINDIA_upperlimit_100 ,
                                     UniversalINDIA_upperlimit_1000 ,
                                     // UniversalAX_upperlimit_3month,
                                     nil];	
        
        
        productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
        productsRequest.delegate = self;
        [productsRequest start];        
        
        
        
    }               
    
}
 
#pragma mark -
#pragma mark 將網路商品取出後的處理

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
	NSLog(@"Received products results...");	
	
	NSArray *products = response.products;
    
	for (int i=0; i<[products count];i++  ) {
		
		proUpgradeProduct =  [[products objectAtIndex:i] retain] ;
		if (proUpgradeProduct)
		{
			
			NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);
			NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);
			NSLog(@"Product price: %@" , proUpgradeProduct.price);
			NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);
			
			[Product_title	 addObject: proUpgradeProduct.localizedTitle];
			[Product_description addObject: proUpgradeProduct.localizedDescription];
			[Product_price addObject: proUpgradeProduct.price];
			[Product_id	addObject: proUpgradeProduct.productIdentifier];
			
			 			
		}		
		
		
	}
	[self.delgete productsRequest_didReceiveResponse];	
	
    for (NSString *invalidProductId in response.invalidProductIdentifiers)
    {
        NSLog(@"Invalid product id: %@" , invalidProductId);
    }
    // finally release the reqest we alloc/init’ed in requestProUpgradeProductData
    [productsRequest release];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];
}

#pragma -
#pragma 通用方法

//
// call this method once on startup
//
- (void)loadStore
{
    // restarts any purchases if they were interrupted last time the app was open
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    
    // get the product description (defined in early sections)
    [self requestProUpgradeProductData];
}

//
// call this before making a purchase
//
- (BOOL)canMakePurchases
{
    return [SKPaymentQueue canMakePayments];
}

#pragma -
#pragma 啟動購買商品
- (void)purchaseProUpgrade:(NSString *)ID
{
    //開始購買
	SKPayment *payment = [SKPayment paymentWithProductIdentifier:ID];
	 
	
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}
 
#pragma mark -
#pragma mark 傳遞交易過程訊號
 
/*很重要 如果沒有這一段 則 app stor 不會回應
   要加在上一層的類別上  [[SKPaymentQueue defaultQueue] addTransactionObserver:IAP];
*/

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
	
    [temp_array_transaction_Restored removeAllObjects];    
    [temp_array__transaction_Restored_rawdata_json removeAllObjects];        
    
    [array_r_bid removeAllObjects];
    [array_r_original_transaction_id removeAllObjects]; 
    [array_r_item_id removeAllObjects];
    [array_r_transaction_id removeAllObjects];
    [array_r_quantity removeAllObjects];
    [array_r_bvrs removeAllObjects];
    [array_r_expires_date_formatted removeAllObjects];
    [array_r_original_purchase_date removeAllObjects];
    [array_r_expires_date removeAllObjects];
    [array_r_purchase_date removeAllObjects];
    [array_r_product_id removeAllObjects];   
    
    [array_lri_bid removeAllObjects];
    [array_lri_original_transaction_id removeAllObjects];
    [array_lri_item_id removeAllObjects];
    [array_lri_transaction_id removeAllObjects];
    [array_lri_quantity removeAllObjects];
    [array_lri_bvrs removeAllObjects];
    [array_lri_expires_date_formatted removeAllObjects];
    [array_lri_original_purchase_date removeAllObjects];
    [array_lri_expires_date removeAllObjects];
    [array_lri_purchase_date removeAllObjects];
    [array_lri_product_id removeAllObjects];        
    
    [array_eri_bid removeAllObjects];
    [array_eri_original_transaction_id removeAllObjects];  
    [array_eri_item_id removeAllObjects];
    [array_eri_transaction_id removeAllObjects];
    [array_eri_quantity removeAllObjects];
    [array_eri_bvrs removeAllObjects];
    [array_eri_expires_date_formatted removeAllObjects];
    [array_eri_original_purchase_date removeAllObjects];
    [array_eri_expires_date removeAllObjects];
    [array_eri_purchase_date removeAllObjects];
    [array_eri_product_id removeAllObjects];      
    
    [array_status removeAllObjects];     
    
    
    
    NSLog(@"paymentQueue");
	int index;
    index=0;
   
   
    
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:{
				
                NSLog(@"StatePurchased 交易成功=(%i/%i)",index,[transactions count] );
                [self completeTransaction:transaction];
                //[queue finishTransaction:transaction];
                 [[SKPaymentQueue defaultQueue] finishTransaction: transaction];                
                break;
            }
            case SKPaymentTransactionStateFailed:{
			
                NSLog(@"StateFailed 交易失敗=(%i/%i)",index,[transactions count]);
                [self failedTransaction:transaction];
                //[queue finishTransaction:transaction]; 
                 [[SKPaymentQueue defaultQueue] finishTransaction: transaction];                
                break;
            }
            case SKPaymentTransactionStateRestored:{
            
                NSLog(@"StateRestored 交易回復=(%i/%i) %@" ,index,[transactions count] ,transaction.payment.productIdentifier);
                
                [temp_array_transaction_Restored addObject:[transaction retain] ];
                
                [self restoreTransaction:transaction];
                //[queue finishTransaction:transaction];
                 [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
                break;
             
                
            }
            default:
                break;
        }
        
        index=index+1;
    }
    
    if ([temp_array_transaction_Restored count]>0) {
       
         
        [delgete start_ActivityIndicator_view:NSLocalizedString(@"Restoring...","回復商品中...") progress_value:temp_array_transaction_Restored_index/[temp_array_transaction_Restored count]];
       
        temp_array_transaction_Restored_index=0;        
        
        NSLog(@"temp_array_transaction_Restored=(%i/%i)  %@",temp_array_transaction_Restored_index,[temp_array_transaction_Restored count]  ,[[(SKPaymentTransaction *)[temp_array_transaction_Restored objectAtIndex:0] payment] productIdentifier]  );  
        
        [self get_all_purchase_information:[temp_array_transaction_Restored objectAtIndex:temp_array_transaction_Restored_index]];	
         temp_array_transaction_Restored_index=temp_array_transaction_Restored_index+1;
    }
    
    
    
    
}
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0){
    NSLog(@"- (void)paymentQueueRestoreCompletedTransactionsFinished");
    
    //如果過去都沒有升級過
    if ([temp_array_transaction_Restored count]==0) {
        UIAlertView *al;
		al=[[UIAlertView alloc] initWithTitle:@"Upgrade Error!" message:@"You have not upgraded this app before." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
		[al show];
        [al release];
    }    
    
    
    
    
}



#pragma mark -
#pragma mark 傳遞交易過程訊號 三種處理模式

- (void)completeTransaction:(SKPaymentTransaction *)transaction{
    NSLog(@"completeTransaction");
	
    [self recordTransaction:transaction];
    
    
    
    [self finishTransaction:transaction wasSuccessful:YES];
	
    //標記:商店購買成功結束後取出發票記錄 記住如果實機測試要修改 #define StoreURL
	[self get_purchase_information:transaction];	
	
} 

- (void)failedTransaction:(SKPaymentTransaction *)transaction{
	NSLog(@"failedTransaction");
	
	if (transaction.error.code != SKErrorPaymentCancelled)
    {
        // error!
        [self finishTransaction:transaction wasSuccessful:NO];
    }
    else
    {
        // this is fine, the user just cancelled, so don’t notify
        [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
    }
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        NSLog(@"self.delgete=%@",self.delgete  );
        [self.delgete productsRequest_failedTransaction];
    }
    
    else{
        [self.delgete productsRequest_failedTransaction];
    }
	    
    
}

- (void)restoreTransaction:(SKPaymentTransaction *)transaction{
    NSLog(@"restoreTransaction");
	 
    //將交易訊息從伺服器上刪除
    [self finishTransaction:transaction wasSuccessful:YES];
      
    
}
 
#pragma mark -
#pragma mark 傳遞交易過程訊號 三種處理模式 交易成功的後續處理
- (void)recordTransaction:(SKPaymentTransaction *)transaction{
   
    NSLog(@"save the transaction receipt to disk");
	//save the transaction receipt to disk
	NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];	    
   
    NSString *jsonObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];	
    //NSLog(@"jsonObjectString=%@",jsonObjectString);
    
    [prefs  setObject:jsonObjectString forKey: @"jsonObjectString"];
    [prefs synchronize];
    
    
}
//交易成功後 將新的最大買入檔數寫入
- (BOOL)isPureInt:(NSString *)string{
    
    NSScanner* scan = [NSScanner scannerWithString:string]; 
    
    int val; 
    
    return [scan scanInt:&val] && [scan isAtEnd];
    
}

//
// removes the transaction from the queue and posts a notification with the transaction result
//
- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful{
    // remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
    
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, @"transaction" , nil];
    if (wasSuccessful)
    {
        // send out a notification that we’ve finished the transaction
        [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionSucceededNotification object:self userInfo:userInfo];
    }
    else
    {
        // send out a notification for the failed transaction
        [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionFailedNotification object:self userInfo:userInfo];
    }
    
	[self.delgete productsRequest_finishTransaction];
	
    NSLog(@"finishTransaction:最後完成交易處理");
}

#pragma mark -
#pragma mark 成功 Non_Consumable後的執行動作
- (void)sucess_Non_Consumable:(NSString *)productId{
	
	NSLog(@"sucess_Non_Consumable=%@",productId); 
	NSLog(@"交易成交回報:一次買斷");        
    
     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];    
    //一次買斷
    NSArray *ccc;
    ccc=[productId componentsSeparatedByString:@"."];
    
    //先檢查之前的最大數目是否小於
    
    int maxsymblos;
    maxsymblos=[[prefs objectForKey:@"MaxSymbolsWatchlist" ] intValue ];			
    
    if ([ccc count]>=4) {
        if ([self isPureInt:[ccc objectAtIndex:3] ]) {
            if (maxsymblos<=[[ccc objectAtIndex:3] intValue ]) {
                
                
                //顯示訊息
                UIAlertView *ia;
                ia=[[UIAlertView alloc] initWithTitle:@"Upgrade Success !" message:[NSString stringWithFormat: @"Each Watchlist is limit to %i symbols now!",[[ccc objectAtIndex:3] intValue ] ] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [ia show];	
                [ia release];	                
                
                //修改新的值
                [prefs setObject:  [NSNumber numberWithInteger:[[ccc objectAtIndex:3] intValue ]] forKey:@"MaxSymbolsWatchlist" ];
                [prefs synchronize];
                
                //更新畫面
                [self.delgete sucess_purchase];		
                
            }
            else {
                
                UIAlertView *ia;
                ia=[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat: @"Each Watchlist is limit to %i symbols now!",maxsymblos ]message:@"It is more than your purchase limit.It will not change the setting. " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [ia show];	
                [ia release];		
                
                
                
            }        
        }
        else{
            UIAlertView *ia;
            ia=[[UIAlertView alloc] initWithTitle:@"Message" message:@"Purchase data error." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [ia show];	
            [ia release];	            
        }
    }
    else{
        UIAlertView *ia;
        ia=[[UIAlertView alloc] initWithTitle:@"Message" message:@"There is no purchase data on this account." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [ia show];	
        [ia release];	    
    }
    
    /*   
    if ([[prefs objectForKey:@"iap_type" ] isEqualToString:@"Auto-Renewable Subscriptions"]) {
        //標記:自動續約 還要取出到期日是多久 並且判斷是否到期
        NSLog(@"交易成交回報:自動續約");
        
    }
    
    else{
                
    }    
    */
    
	
	
	
}

#pragma mark -
#pragma mark 成功 Auto_Renewable後的執行動作

//標記:以網頁HTTP url方式 POST資料

-(void)success_Auto_Renewable:(NSString *)update_expire_date expire_datestring:(NSString *)update_expire_date_unixstring {
    
    
    NSString *BundleIdentifier;
    BundleIdentifier=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];     
    
    //取出device 資料
	UIDevice *device = [UIDevice currentDevice];
	NSString *device_uniqueIdentifier = [device uniqueIdentifier];    
    
    //另外php 為record_login.php
    
    NSURL *url = [NSURL URLWithString:@"http://www.xxtradexx.com/php_learn/record_update_iap_expire.php"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];    
    
    NSLog(@"Begin request");
    
    [request setPostValue:device_uniqueIdentifier forKey:@"udid"];
    [request setPostValue:BundleIdentifier forKey:@"productid"];    
        
    
    [request setPostValue:update_expire_date forKey:@"update_expire_date"];   
    [request setPostValue:update_expire_date_unixstring forKey:@"update_expire_date_unix"];    
    
    /*
    [request setPostValue:r_bid forKey:@"r_bid"];     
    [request setPostValue:r_original_transaction_id forKey:@"r_original_transaction_id"];   
    [request setPostValue:r_item_id forKey:@"r_item_id"];     
    [request setPostValue:r_transaction_id forKey:@"r_transaction_id"]; 
    [request setPostValue:r_quantity forKey:@"r_quantity"];     
    [request setPostValue:r_bvrs forKey:@"r_bvrs"]; 
    [request setPostValue:r_expires_date_formatted forKey:@"r_expires_date_formatted"];     
    [request setPostValue:r_original_purchase_date forKey:@"r_original_purchase_date"]; 
    [request setPostValue:r_expires_date forKey:@"r_expires_date"];     
    [request setPostValue:r_purchase_date forKey:@"r_purchase_date"]; 
    [request setPostValue:r_product_id forKey:@"r_product_id"];     
     
    [request setPostValue:lri_bid forKey:@"lri_bid"];     
    [request setPostValue:lri_original_transaction_id forKey:@"lri_original_transaction_id"];   
    [request setPostValue:lri_item_id forKey:@"lri_item_id"];     
    [request setPostValue:lri_transaction_id forKey:@"lri_transaction_id"]; 
    [request setPostValue:lri_quantity forKey:@"lri_quantity"];     
    [request setPostValue:lri_bvrs forKey:@"lri_bvrs"]; 
    [request setPostValue:lri_expires_date_formatted forKey:@"lri_expires_date_formatted"];     
    [request setPostValue:lri_original_purchase_date forKey:@"lri_original_purchase_date"]; 
    [request setPostValue:lri_expires_date forKey:@"lri_expires_date"];     
    [request setPostValue:lri_purchase_date forKey:@"lri_purchase_date"]; 
    [request setPostValue:lri_product_id forKey:@"lri_product_id"];    
    
    [request setPostValue:eri_bid forKey:@"eri_bid"];     
    [request setPostValue:eri_original_transaction_id forKey:@"eri_original_transaction_id"];   
    [request setPostValue:eri_item_id forKey:@"eri_item_id"];     
    [request setPostValue:eri_transaction_id forKey:@"eri_transaction_id"]; 
    [request setPostValue:eri_quantity forKey:@"eri_quantity"];     
    [request setPostValue:eri_bvrs forKey:@"eri_bvrs"]; 
    [request setPostValue:eri_expires_date_formatted forKey:@"eri_expires_date_formatted"];     
    [request setPostValue:eri_original_purchase_date forKey:@"eri_original_purchase_date"]; 
    [request setPostValue:eri_expires_date forKey:@"eri_expires_date"];     
    [request setPostValue:eri_purchase_date forKey:@"eri_purchase_date"]; 
    [request setPostValue:eri_product_id forKey:@"eri_product_id"];     
    */
    
    [request setRequestMethod:@"POST"];
     NSLog(@"End request");    
    
    [request setValidatesSecureCertificate:NO];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(record_update_iap_expire_Finish:)];
    [request setDidFailSelector:@selector(record_update_iap_expire_Fail:)];    
    [request startAsynchronous];
    
    
    
    
    
}

-(void)record_update_iap_expire_Finish:(ASIHTTPRequest *)request{
    NSLog(@"record_update_iap_expire_Finish");
    [self.delgete  productsRequest_check_iap_expire_date ]; 
   
    
    
}
-(void)record_update_iap_expire_Fail:(ASIHTTPRequest *)request{
    NSLog(@"record_check_iap_expire_Fail"); 
   
    
}





#pragma mark -
#pragma mark 購買資訊收據取的

//用在回復歷史購買商品訊息所使用
 

-(void)get_all_purchase_information:(SKPaymentTransaction *)transaction{
        
    NSString *jsonObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];	
        NSString *json;
        NSString *pass;
        pass=@"063f60044e0c4f36b9cc13ba410c7d3d";
        json=[NSString stringWithFormat:@"{\"receipt-data\":\"%@\",\"password\":\"%@\"}",jsonObjectString,pass];
        
         
        NSData *data;
        data=[json dataUsingEncoding:NSUTF8StringEncoding];
        
        NSMutableURLRequest *request;
        NSURL *sandboxStoreURL = [[NSURL alloc] initWithString:StoreURL ];
        
        request=[NSMutableURLRequest requestWithURL:sandboxStoreURL];
    [sandboxStoreURL release];
    
       [request setHTTPBody:data];
        [request setHTTPMethod:@"POST"];
        
        
        isRestoreHistoryRecipt=YES;
        decodedData=[[NSMutableData data] retain];
        connection=[NSURLConnection connectionWithRequest:request delegate:self];        
   
   
}
//主程式
//單次購買訊息取出
-(void)get_purchase_information:(SKPaymentTransaction *)transaction{
    NSString *jsonObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];	
	NSString *json;
	NSString *pass;
    pass=@"063f60044e0c4f36b9cc13ba410c7d3d";
	json=[NSString stringWithFormat:@"{\"receipt-data\":\"%@\",\"password\":\"%@\"}",jsonObjectString,pass];
 	
	NSData *data;
	data=[json dataUsingEncoding:NSUTF8StringEncoding];
	
	NSMutableURLRequest *request;
	NSURL *sandboxStoreURL = [[NSURL alloc] initWithString:StoreURL ];
	
	request=[NSMutableURLRequest requestWithURL:sandboxStoreURL];
	[sandboxStoreURL release];
    
    [request setHTTPBody:data];
	[request setHTTPMethod:@"POST"];
	
    isRestoreHistoryRecipt=NO;
	decodedData=[[NSMutableData data] retain];
	connection=[NSURLConnection connectionWithRequest:request delegate:self];
}
//後續相關解析程式
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    // append the new data to the receivedData
	//lab.text=@"downloda...";
	[decodedData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
	//分兩段
    //第一段:將下載資料解析
	NSString *receipt;
	receipt=[[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
	
    NSLog(@"all information=%@",receipt);
	
    [temp_array__transaction_Restored_rawdata_json addObject:receipt];
    
	NSMutableDictionary *d = [receipt JSONValue];
    
	
	
    //如何判斷收據是Auto_Renewable 或是 consumable
    //因為如果是Auto_Renewable 會多出 不是latest_receipt_info 就是latest_expired_receipt_info 兩種訊息 所以就用此來判斷目前的收據形態
    
   // BOOL isAuto_Renewable;
    //isAuto_Renewable=NO;
    
    NSString *receipt_product_id;
    NSString *latest_receipt_info_expires_date_formatted;   
    NSString *latest_receipt_info_expires_date_unixstring;    
    //數據資料分析 收據資料種類有三種
    NSMutableDictionary *rl=[d objectForKey:@"latest_receipt_info"];
	for (NSString *s in [rl allKeys] ) {
	   // isAuto_Renewable=YES;
        NSLog(@"latest_receipt_info:%@=%@",s,[rl objectForKey:s ]);
        
        if (  [s isEqualToString:@"product_id"] ) {
            
            receipt_product_id= [rl objectForKey:s ]  ;           
            
        }	        
        if (  [s isEqualToString:@"expires_date_formatted"] ) {
            
                     
            
        }	  
        
        
        
        if (  [s isEqualToString:@"bid"] ) {
            lri_bid= [rl  objectForKey:s ]   ;   
            [array_lri_bid  addObject:[rl  objectForKey:s ]];         
        }	         
        if (  [s isEqualToString:@"original_transaction_id"] ) {
            lri_original_transaction_id= [rl  objectForKey:s ]   ;           
            [array_lri_original_transaction_id  addObject:[rl  objectForKey:s ]];     
        }
        if (  [s isEqualToString:@"item_id"] ) {
            lri_item_id= [rl   objectForKey:s ]   ; 
            [array_lri_item_id  addObject:[rl  objectForKey:s ]];             
        }
        if (  [s isEqualToString:@"transaction_id"] ) {
            lri_transaction_id= [rl  objectForKey:s ]   ;  
            [array_lri_transaction_id  addObject:[rl  objectForKey:s ]];             
        }        
        if (  [s isEqualToString:@"quantity"] ) {
            lri_quantity= [rl  objectForKey:s ]  ;  
            [array_lri_quantity  addObject:[rl  objectForKey:s ]];             
        }              
        if (  [s isEqualToString:@"bvrs"] ) {
            lri_bvrs= [rl  objectForKey:s ]   ; 
            [array_lri_bvrs  addObject:[rl  objectForKey:s ]]; 
        }
        if (  [s isEqualToString:@"original_purchase_date"] ) {
            lri_original_purchase_date= [rl  objectForKey:s ] ;
            [array_lri_original_purchase_date  addObject:[rl  objectForKey:s ]]; 
        }        
        if (  [s isEqualToString:@"expires_date"] ) {
            lri_expires_date= [rl  objectForKey:s ]  ; 
            [array_lri_expires_date  addObject:[rl  objectForKey:s ]];  
            //標記:因為收據的時間資料 有毫秒 但是nsdate 是以秒為計算單位 因此必須要再次調整
            double ie ;
            ie= [[rl objectForKey:s ] doubleValue]/1000;
            latest_receipt_info_expires_date_unixstring= [NSString stringWithFormat:@"%.0f",ie]  ;             
        }              
        if (  [s isEqualToString:@"purchase_date"] ) {
            lri_purchase_date= [rl  objectForKey:s ]  ;  
            [array_lri_purchase_date  addObject:[rl  objectForKey:s ]];             
        }
        if (  [s isEqualToString:@"product_id"] ) {
            lri_product_id= [rl  objectForKey:s ]   ; 
            [array_lri_product_id  addObject:[rl  objectForKey:s ]];             
        }
        if (  [s isEqualToString:@"expires_date_formatted"] ) {
            lri_expires_date_formatted= [rl  objectForKey:s ]  ;   
            [array_lri_expires_date_formatted  addObject:[rl  objectForKey:s ]]; 
       
            latest_receipt_info_expires_date_formatted= [[rl objectForKey:s ] retain]   ;         
        }          
        
        
        
        
        
    }    
    
    NSMutableDictionary *rel=[d objectForKey:@"latest_expired_receipt_info"];
	for (NSString *s in [rel allKeys] ) {
		//isAuto_Renewable=YES;
        if (  [s isEqualToString:@"product_id"] ) {
            
            receipt_product_id= [rel objectForKey:s ]   ;           
            
        }	
        
        
        
        
        if (  [s isEqualToString:@"bid"] ) {
            eri_bid= [rel  objectForKey:s ]  ; 
            [array_eri_bid  addObject:[rel  objectForKey:s ]];             
            
        }	         
        if (  [s isEqualToString:@"original_transaction_id"] ) {
            eri_original_transaction_id= [rel  objectForKey:s ]  ; 
            [array_eri_original_transaction_id  addObject:[rel  objectForKey:s ]];              
        }
        if (  [s isEqualToString:@"item_id"] ) {
            eri_item_id= [rel   objectForKey:s ]   ;  
            [array_eri_item_id  addObject:[rel  objectForKey:s ]];              
        }
        if (  [s isEqualToString:@"transaction_id"] ) {
            eri_transaction_id= [rel  objectForKey:s ]  ;  
            [array_eri_transaction_id  addObject:[rel  objectForKey:s ]];              
        }        
        if (  [s isEqualToString:@"quantity"] ) {
            eri_quantity= [rel  objectForKey:s ]   ;  
            [array_eri_quantity  addObject:[rel  objectForKey:s ]];              
        }              
        if (  [s isEqualToString:@"bvrs"] ) {
            eri_bvrs= [rel  objectForKey:s ]   ;  
            [array_eri_bvrs  addObject:[rel  objectForKey:s ]];              
        }
        if (  [s isEqualToString:@"original_purchase_date"] ) {
            eri_original_purchase_date= [rel  objectForKey:s ]   ;           
            [array_eri_original_purchase_date  addObject:[rel  objectForKey:s ]];          
        }        
        if (  [s isEqualToString:@"expires_date"] ) {
            eri_expires_date= [rel  objectForKey:s ]   ; 
            [array_eri_expires_date  addObject:[rel  objectForKey:s ]];              
        }              
        if (  [s isEqualToString:@"purchase_date"] ) {
            eri_purchase_date= [rel  objectForKey:s ]  ;   
            [array_eri_purchase_date  addObject:[rel  objectForKey:s ]];  
        }
        if (  [s isEqualToString:@"product_id"] ) {
            eri_product_id= [rel  objectForKey:s ]  ;    
            [array_eri_product_id  addObject:[rel  objectForKey:s ]];  
        }
        if (  [s isEqualToString:@"expires_date_formatted"] ) {
            eri_expires_date_formatted= [rel  objectForKey:s ]   ;           
            [array_eri_expires_date_formatted  addObject:[rel  objectForKey:s ]];          
        }          
        
        NSLog(@"latest_expired_receipt_info:%@=%@",s,[rel objectForKey:s ]);
	}       
         
     
    NSMutableDictionary *r=[d objectForKey:@"receipt"];	
    
    for (NSString *s in [r allKeys] ) {
		NSLog(@"receipt:%@=%@",s,[r objectForKey:s ]);
        if (  [s isEqualToString:@"product_id"] ) {
              receipt_product_id= [r  objectForKey:s ]   ;    
                        
            
        }	         
        if (  [s isEqualToString:@"bid"] ) {
             r_bid= [r  objectForKey:s ]  ;  
             [array_r_bid addObject:[r  objectForKey:s ]];
        }	         
        if (  [s isEqualToString:@"original_transaction_id"] ) {
              r_original_transaction_id= [r  objectForKey:s ]  ; 
              [array_r_original_transaction_id addObject:[r  objectForKey:s ]];
        }
        if (  [s isEqualToString:@"item_id"] ) {
              r_item_id= [r  objectForKey:s ]  ;  
              [array_r_item_id addObject:[r  objectForKey:s ]];
        }
        if (  [s isEqualToString:@"transaction_id"] ) {
             r_transaction_id= [r  objectForKey:s ]  ;
              [array_r_transaction_id addObject:[r  objectForKey:s ]];            
        }        
        if (  [s isEqualToString:@"quantity"] ) {
            r_quantity= [r  objectForKey:s ]  ; 
            [array_r_quantity addObject:[r  objectForKey:s ]];            
        }              
        if (  [s isEqualToString:@"bvrs"] ) {
            r_bvrs= [r  objectForKey:s ]   ; 
            [array_r_bvrs addObject:[r  objectForKey:s ]];            
        }
        if (  [s isEqualToString:@"original_purchase_date"] ) {
            r_original_purchase_date= [r  objectForKey:s ]   ;           
            [array_r_original_purchase_date addObject:[r  objectForKey:s ]];        
        }        
        if (  [s isEqualToString:@"expires_date"] ) {
            r_expires_date= [r  objectForKey:s ]   ;   
            [array_r_expires_date addObject:[r  objectForKey:s ]];
        }              
        if (  [s isEqualToString:@"purchase_date"] ) {
            r_purchase_date= [r  objectForKey:s ]   ;  
            [array_r_purchase_date addObject:[r  objectForKey:s ]];            
            
        }
        if (  [s isEqualToString:@"product_id"] ) {
            r_product_id= [r  objectForKey:s ]   ;  
            [array_r_product_id addObject:[r  objectForKey:s ]];            
        }
        if (  [s isEqualToString:@"expires_date_formatted"] ) {
            r_expires_date_formatted= [r  objectForKey:s ]   ;  
            [array_r_expires_date_formatted addObject:[r  objectForKey:s ]];            
        }          
               
	}
    
    [array_status addObject:[d objectForKey:@"status"]];
    
     
    
  
    //第二段:進行判斷與啓動功能
    if (isRestoreHistoryRecipt==YES    ) {
        NSLog(@"表示目前是 回復歷史購買 數據資料"); 
        //如果是回復歷史購買資料因為有多筆 所以要回圈呼叫       
        //尚未全部取出 多筆收據 所以要再次呼叫
        if ([temp_array_transaction_Restored count]>temp_array_transaction_Restored_index) {
            float prv;
            float a;
            float b;
            a=[ [NSString stringWithFormat:@"%i", temp_array_transaction_Restored_index] floatValue ];
            b=[ [NSString stringWithFormat:@"%i", [temp_array_transaction_Restored count]] floatValue ];
            prv=a/b;
            NSLog(@"fff=%f",prv);
            
            [delgete start_ActivityIndicator_view:NSLocalizedString(@"Restoring...","回復商品中...") progress_value:prv];  
            
            NSLog(@"temp_array_transaction_Restored=(%i/%i)   %@",temp_array_transaction_Restored_index,[temp_array_transaction_Restored count]   ,[[(SKPaymentTransaction *)[temp_array_transaction_Restored objectAtIndex:temp_array_transaction_Restored_index] payment] productIdentifier]  );  
            
            //執行取出多筆數據資料
            [self get_all_purchase_information:[temp_array_transaction_Restored objectAtIndex:temp_array_transaction_Restored_index]];	
            temp_array_transaction_Restored_index=temp_array_transaction_Restored_index+1;
            
        }            
        else{
           //已經全部回收所有購買資訊     
            [delgete stop_ActivityIndicator_view];            
            
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
            
            if ([[prefs objectForKey:@"iap_type" ] isEqualToString:@"Auto-Renewable Subscriptions"]) {
                NSLog(@"表示已經取出所有的歷史購買資料:自動續約");
                
                //開始從取出尚未到期的收據裡 找出最後到期日
                int last_expirate_date;
                NSString *last_expirate_date_formated;
                NSString *last_expirate_date_unixstring;                
                NSString *last_expirate_date_product_id; 
                last_expirate_date=0;
                if ([array_lri_product_id count]>0) {
                    //進入回圈比較
                    for (int i=0; i<[array_lri_product_id count]; i++) {
                        NSLog(@"lri=%@ %@ %@",[array_lri_product_id objectAtIndex:i],[array_lri_expires_date_formatted objectAtIndex:i],[array_lri_expires_date objectAtIndex:i] );
                        //判斷是不是有更後面的到期日
                        if ([[array_lri_expires_date objectAtIndex:i] intValue] >last_expirate_date) {
                            last_expirate_date=[[array_lri_expires_date objectAtIndex:i] intValue];
                            last_expirate_date_formated=[array_lri_expires_date_formatted objectAtIndex:i];
                            last_expirate_date_product_id=  [array_lri_product_id objectAtIndex:i];
                            last_expirate_date_unixstring= [array_lri_expires_date objectAtIndex:i];                           
                        }
                        
                    }             
                    
                    NSLog(@"回復訂閱結果:到期日:%@",last_expirate_date_formated);
                    NSLog(@"回復訂閱結果:到期日unix:%i",last_expirate_date);                    
                    NSLog(@"回復訂閱結果:product_id:%@",last_expirate_date_product_id);
                    
                    //將資料上傳到網路伺服器上儲存
                    [self success_Auto_Renewable:last_expirate_date_formated expire_datestring:last_expirate_date_unixstring ];                    
                }
                else{
                    NSLog(@"回復訂閱結果:目前所有訂閱都到期");
                    
                    //顯示訊息
                    UIAlertView *ia;
                    ia=[[UIAlertView alloc] initWithTitle:@"Restore Fail !" message:@"It has expired !!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [ia show];	
                    [ia release];	                       
                    
                }                                                                               
                
                
                /*
                 if ([array_eri_product_id count]>0) {
                 for (int i=0; i<[array_eri_product_id count]; i++) {
                 NSLog(@"eri=%@ %@ %@",[array_eri_product_id objectAtIndex:i],[array_eri_original_transaction_id objectAtIndex:i],[array_eri_transaction_id objectAtIndex:i] );
                 } 
                 }        
                 */        
                //original_transaction_id
                //purchase_date:交易發生日或是最近一筆更新日
                //original_purchase_date:原始第一次購買日
                //expires_date:到期日   
                
                
                
                
                         
            }
            else{
                NSLog(@"表示已經取出所有的歷史購買資料:一次買斷");        
                int max_stock;
                max_stock=0;
                NSString *max_product_id;
                max_product_id=@"";                
                
                if ([array_r_product_id count]>0) {
                    NSLog(@"之前有購買過");
                    
                    
                    for (int i=0; i<[array_r_product_id count]; i++) {
                        NSLog(@"r=%@ %@ %@",[array_r_product_id objectAtIndex:i],[array_r_original_transaction_id objectAtIndex:i],[array_r_purchase_date objectAtIndex:i] );
                        
                        NSArray * Timestamp_array=[[array_r_product_id objectAtIndex:i] componentsSeparatedByString:@"."];                         
                        
                        if ([Timestamp_array count]>=4) {
                            if ([[Timestamp_array objectAtIndex:3] intValue]>max_stock ) {
                                max_stock=[[Timestamp_array objectAtIndex:3] intValue];
                                max_product_id=[array_r_product_id objectAtIndex:i];
                           
                            }           
                        }
                       
                        
                    } 
                    NSLog(@"回復一次買斷結果:最大檔數:%i",max_stock) ;
                    NSLog(@"回復一次買斷結果:product_id:%@",max_product_id);                    
                    //一次買斷成功回復後是將結果寫到機器上
                    [self sucess_Non_Consumable:max_product_id];                    
                    
                    
                }
                else{
                    NSLog(@"之前無購買過");
                }
                
            }                 
             
        }
        
    }    
    else{
      NSLog(@"表示目前是 購買後 的數據資料"); 
       //等驗證過後 確定用可以分開url 判斷後 要將購買成功後的功能寫在這邊  
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        
        if ([[prefs objectForKey:@"iap_type" ] isEqualToString:@"Auto-Renewable Subscriptions"]) {
            //標記:自動續約 還要取出到期日是多久 並且判斷是否到期
            NSLog(@"取出購買後的數據資料:交易成交回報:自動續約=%@",latest_receipt_info_expires_date_formatted);
            [self success_Auto_Renewable:latest_receipt_info_expires_date_formatted expire_datestring:latest_receipt_info_expires_date_unixstring ];            
        }
        else{
            NSLog(@"取出購買後的數據資料:交易成交回報:一次買斷=%@",receipt_product_id);        
            //一次買斷
            [self sucess_Non_Consumable:receipt_product_id];                       
        }            
         
        
    }
     
	 
    [receipt release];       
          
	//識別碼
	UIDevice *device = [UIDevice currentDevice];
	NSString *uniqueIdentifier = [device uniqueIdentifier];
	
	NSLog(@"guid=%@",uniqueIdentifier);	
	 
}

-(NSString *)CpathForSymbol:(NSString *)aSymbol{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *docPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_C.plist", aSymbol]];
	return docPath;
}

- (NSString *)encode:(const uint8_t *)input length:(NSInteger)length {
    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	
    NSMutableData *data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t *output = (uint8_t *)data.mutableBytes;
	
    for (NSInteger i = 0; i < length; i += 3) {
        NSInteger value = 0;
        for (NSInteger j = i; j < (i + 3); j++) {
			value <<= 8;
			
			if (j < length) {
				value |= (0xFF & input[j]);
			}
        }
		
        NSInteger index = (i / 3) * 4;
        output[index + 0] =                    table[(value >> 18) & 0x3F];
        output[index + 1] =                    table[(value >> 12) & 0x3F];
        output[index + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[index + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }
	
    return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
}




@end
